<template>
  <MainSlot>
    <template v-slot:searchForm>
      <SearchForm v-if="formItemList.length" :formItemList="formItemList" :unfoldFlag="unfoldFlag" @search="search"
        @isUnfold="(v) => { unfoldFlag = v }">
      </SearchForm>
    </template>
    <template v-slot:btn>
      <BtnSlot :btnList="btnList" @handleEvent="getBtnEvent"></BtnSlot>
    </template>
    <template v-slot:table>
      <TableData :config="table_config" :tableThead="tableThead" :dataList="tableData" :loading="loading"
        @selection-change="handleSelectionChange">
        <template v-slot:dataItemName="{ row }">
          <span v-if="row.dataItem">{{ row.dataItem.dataItemName || '--' }}</span>
          <span v-else>--</span>
        </template>
        <template v-slot:mainingControlTypeStr="{ row }">
          <span v-if="row.dataItem && row.dataItem.dataType">{{ row.dataItem.dataType.mainingControlTypeStr || '--'
            }}</span>
          <span v-else>--</span>
        </template>
      </TableData>
    </template>
    <template v-slot:pagination>
      <Pagination :size="pageSize" :page="currentPage" :total="totalSize" @current-change="changeCurrentPage" />
    </template>
    <template v-slot:dialogModule>
      <AddEditDialog ref="addEditDialog" :now-chance-stau="nowChanceStau" :title-text="titleText"
        :edit-object="editObject" :show.sync="showAddEditDialog" @closeDialog="closeDialog" @cancelDialog="cancel" />
    </template>
  </MainSlot>
</template>
<script>
import MainSlot from "@/components-production-management/MainSlot";
import Pagination from '@/components-production-management/Pagination';
import SearchForm from '@/components-production-management/SearchForm';
import TableData from '@/components-production-management/TableData';
import BtnSlot from '@/components-production-management/BtnSlot';
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from 'vuex';

import EnvironmentalDataEntryApi from "@/api/iot-monitoring/environmental-data-entry";
export default {
  name: 'AataCategoryManualRecording',
  components: { Pagination, MainSlot, AddEditDialog, TableData, BtnSlot, SearchForm },
  mixins: [mymixin],
  data() {
    return {
      loading: false,
      unfoldFlag: false,
      btnList: ['add', 'delete'],
      searchFormData: {
        dataItemName: ''
      },
      checkedIdList: [],
      tableThead: [
        { isShow: true, label: '采集时间', prop: 'collectDatetime' },
        { isShow: true, label: '采集值', prop: 'value' },
        { isShow: true, label: '数据项', slotName: 'dataItemName' },
        { isShow: true, label: '数据项类型', slotName: 'mainingControlTypeStr' },
      ],
      formItemList: [
        {
          label: '数据项名称',
          type: 'input',
          param: 'dataItemName',
          multiple: false,
          isShow: true
        }
      ],
      // 配置项
      table_config: {
        isCheckBox: true,

        isOperation: true,
        // 表格操作列
        operation: {
          label: '操作',
          width: '200',
          cols: [
            {
              label: '编辑',
              type: 'primary',
              flag: 'edit',
              handleRow: this.handleRow
            },
            {
              label: '删除',
              type: 'danger',
              flag: 'del',
              handleRow: this.handleRow
            }
          ]
        },
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission"
    })
  },

  watch: {},
  created() {
    this.queryPageList();
  },
  mounted() {
  },
  methods: {
    // 测试数据类别分页查询接口
    queryPageList() {
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          ...this.searchFormData
        }
      };
      this.loading = true;
      EnvironmentalDataEntryApi.queryHistoryPageByCondition(params).then(res => {
        this.loading = false;
        if (res.success === true) {
          let data = res.data || {};
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
        }
      }).catch(()=>{
        this.loading = false;
      });
    },
    search(data) {
      this.page = 1;
      this.searchFormData = { ...data }
      this.queryPageList();
    },
    // table操作方法
    handleRow(rowData, item) {
      if (item.flag == 'edit') {
        this.edit(rowData)
      } else if (item.flag == 'del') {
        this.delOne(rowData)
      }
      this.addEditFlag = item.flag
    },
    //获取选中Btn
    getBtnEvent(flag) {
      if (flag == 'add') {
        this.add();
      } else if (flag == 'delete') {
        if (this.checkedIdList.length > 0) {
          this.delmessage(this.checkedIdList);
        } else {
          this.$message.warning('请先选择表单数据项后再删除！');
        }
      };
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.queryPageList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.queryPageList();
    },
    selectAll(list) {
      let notvalue = true
      list.forEach(item => {
        if (!item.dataCollectHistoryId) {
          notvalue = false
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true
      list.forEach(item => {
        if (!item.dataCollectHistoryId) {
          notvalue = false
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true
      let emptyList = []
      this.checkedIdList = list.map(item => {
        if (item.dataCollectHistoryId) {
          return item.dataCollectHistoryId
        } else {
          notvalue = false
          emptyList.push(item)
          return ""
        }
      });
      if (notvalue === false) {
        this.toggleNoSelection(emptyList)
        return
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      })
        .then(() => {
          EnvironmentalDataEntryApi.delById({ dataCollectHistoryId: data.dataCollectHistoryId }).then(res => {
            if (res.success) {
              this.$message.success("删除成功");
              this.queryPageList();
              this.resetActionBar();
            }
          });
        })
        .catch(() => {
          this.resetActionBar();
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 表格删除
    delmessage() {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        
      }).then(() => {
        EnvironmentalDataEntryApi.delListById({ dataCollectHistoryIdList: this.checkedIdList }).then(res => {
          if (res.success) {
            this.$message.success("删除成功");
            this.queryPageList();
            this.resetActionBar();
          } else {
            this.$message.warning(res.message);
          }
        });
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      })
    },
    // 添加
    add() {
      this.nowChanceStau = 'add'
      this.titleText = "添加"
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true
    },
    // 修改
    edit(row) {
      if (row.dataCollectHistoryId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = 'edit'
        this.titleText = "修改"
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true
      } else {
        this.editObject = {}
      }
    }
  }
};
</script>

<style lang="scss" scoped></style>
